﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Reflection;
/*
 * Author:W
 * xml文件解析
 */
namespace W.GameFrameWork.ExcelTool
{
	public class XmlHelper
	{
		/// <summary>
		/// 读取xml文件数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="filePath"></param>
		/// <returns></returns>
		public static List<T> ReadXmlFile<T>(string filePath)
		{
			System.Type classType = typeof(T);
			Assembly assembly = Assembly.GetAssembly(classType);//获取泛型的程序集
			List<FieldInfo> fieldInfos = new List<FieldInfo>(classType.GetFields());//获取到泛型所有字段的集合

			List<T> objList = new List<T>();

			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(filePath);
			XmlNodeList rowNodeList = xmlDocument.SelectNodes("Table/Row");			

			for (int i = 0; i < rowNodeList.Count; i++)
			{
				XmlNodeList fieldNodeList = rowNodeList[i].ChildNodes;
				object obj = assembly.CreateInstance(classType.FullName); //泛型实例化
				

				for (int j = 0; j < fieldNodeList.Count; j++)
				{
					FieldInfo fieldInfo = fieldInfos.Find(t => t.Name.Equals(fieldNodeList[j].Name));
										
					if (fieldInfo != null)
					{						
						XmlNodeList valueArr;

						if (fieldInfo.FieldType.Equals(typeof(System.Int32[])))
						{
                            valueArr = fieldNodeList[j].ChildNodes;
                            int[] intArr = new int[valueArr.Count];
                            for (int m = 0; m < valueArr.Count; m++)
                            {
                                intArr[m] = int.Parse(valueArr[m].InnerText);
                            }

							fieldInfo.SetValue((T)obj, intArr);
                        }
						else if (fieldInfo.FieldType.Equals(typeof(System.String[])))
						{
                            valueArr = fieldNodeList[j].ChildNodes;

                            string[] strArr = new string[valueArr.Count];
                            for (int m = 0; m < valueArr.Count; m++)
                            {
                                strArr[m] = valueArr[m].InnerText;
                            }

                            fieldInfo.SetValue((T)obj, strArr);
                        }
						else if (fieldInfo.FieldType.Equals(typeof(System.Single[])))
						{
                            valueArr = fieldNodeList[j].ChildNodes;
                            float[] floatArr = new float[valueArr.Count];

                            for (int m = 0; m < valueArr.Count; m++)
                            {
                                floatArr[m] = float.Parse(valueArr[m].InnerText);
                            }

                            fieldInfo.SetValue((T)obj, floatArr);

                        }
						else if (fieldInfo.FieldType.Equals(typeof(int)))
						{
							fieldInfo.SetValue((T)obj, int.Parse(fieldNodeList[j].InnerText));
						}
						else if (fieldInfo.FieldType.Equals(typeof(string)))
						{                            
                            fieldInfo.SetValue((T)obj, fieldNodeList[j].InnerText);
                        }
						else if (fieldInfo.FieldType.Equals(typeof(float)))
						{
							fieldInfo.SetValue((T)obj, float.Parse(fieldNodeList[j].InnerText));
						}

					}
				}

				objList.Add((T)obj);
			}			

			return objList;
		}
	}
}

